﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DSDCiZharp.Models.Reserva;

namespace DSDCiZharp.Controllers
{
    public class PagosController : Controller
    {
        //
        // GET: /Pagos/

        public ActionResult Pagos(/*int idCliente*/)
        {
            if (Session["Id_Cliente"] != null)
            {
                List<Reserva> lstReservas = new List<Reserva>();

                //idCliente = ;
                var lstReservasService = new PagoServiceReference.PagoServiceClient().ListReservas(((Cliente)Session["Id_Cliente"]).ID_CLIENTE);

                foreach (DSDCiZharp.PagoServiceReference.Reserva rrsva in lstReservasService)
                {
                    lstReservas.Add(new Reserva()
                    {
                        COSTO_TOTAL = rrsva.COSTO_TOTAL,
                        ESTADO_PAGO = rrsva.ESTADO_PAGO,
                        ID_CLIENTE = rrsva.ID_CLIENTE,
                        ID_RESERVA = rrsva.ID_RESERVA
                    });

                }

                return View("Pagos", lstReservas);
            }
            else
            {
                
                Session["ErrorLoginPagos"] = "Debe Estar Logueado para entrar a esta seccion";
                return RedirectToAction("Index", "Venta");

            }
        }

        #region Acciones Creadas por Defecto

        //
        // GET: /Pagos/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Pagos/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Pagos/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Pagos/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Pagos/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Pagos/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Pagos/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        #endregion

        #region Acciones Personalisadas

        //
        // GET: /Pagos/Pagar/5

        public ActionResult Pagar(int idReserva, int idCliente)
        {
            PagoTarjetaModel datosPago = new PagoTarjetaModel() { IdCliente = idCliente, IdReserva = idReserva };

            return View(datosPago);
        }

        //
        // POST: /Pagos/Pagar/5

        [HttpPost]
        public ActionResult Pagar(PagoTarjetaModel datosPagoConTarjeta)
        {
            try
            {
                PagoServiceReference.PagoTarjetaModel datosPagoConTarjetaService = new PagoServiceReference.PagoTarjetaModel() 
                                                                                   { ClaveDigital = datosPagoConTarjeta.ClaveDigital,
                                                                                     IdCliente = datosPagoConTarjeta.IdCliente,
                                                                                     IdReserva = datosPagoConTarjeta.IdReserva,
                                                                                     NumeroTarjeta = datosPagoConTarjeta.NumeroTarjeta,
                                                                                     Token = datosPagoConTarjeta.Token};

                string messageRespuesta = new PagoServiceReference.PagoServiceClient().PagarReserva(datosPagoConTarjetaService);
                ViewData["MessageRespuesta"] = messageRespuesta;

                if (messageRespuesta.Equals("Pago no procesado"))
                {
                    ViewData["CodigoPago"] = "----";
                }
                else
                {
                    ViewData["CodigoPago"] = datosPagoConTarjeta.IdReserva.ToString() + datosPagoConTarjeta.Token;
                }

                return View("Comprobante");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Comprobante(int idReserva)
        {
           
            return View();
        }


        #endregion
    }
}
